
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hippo_app/router_table.dart';
import 'package:hippo_app/values/color_value.dart';
import 'package:hippo_app/values/style_value.dart';

class LaunchScreenPage extends StatefulWidget {
  const LaunchScreenPage({super.key});

  @override
  State<LaunchScreenPage> createState() => _LaunchScreenPageState();
}

class _LaunchScreenPageState extends State<LaunchScreenPage>  with SingleTickerProviderStateMixin {

  Duration keepTimer = const Duration(seconds: 1);

  int get showSecond => keepTimer.inSeconds;

  late Timer timer;

  toPage() {
    Navigator.of(context).pushNamedAndRemoveUntil(RouterTable.appPath, (route) => false);
  }

  @override
  void initState() {
    super.initState();

    Duration step = const Duration(seconds: 1);
    timer = Timer.periodic(const Duration(seconds: 1), (_) {
      setState(() {
        keepTimer -= step;
      });
      if (keepTimer == Duration.zero) {
        timer.cancel();
        toPage();
      }
    });
  }

  @override
  void dispose() {
    timer.cancel();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [SystemUiOverlay.top]);
    return Scaffold(
      backgroundColor: ColorValue.launchScreenBg,
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          Positioned(
            right: 10,
            bottom: 10,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextButton(
                onPressed: toPage,
                child: Text('跳过${showSecond > 0 ? showSecond : ''}',style: StyleValue.launchScreenSecondStyle,),
              ),
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: 110.0,
                height: 110.0,
                padding: const EdgeInsets.all(16.0),
                decoration: const BoxDecoration(
                    color: ColorValue.launchLogoBg,
                    borderRadius: BorderRadius.all(Radius.circular(60))),
                child: Image.asset('assets/images/logo.png'),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10.0),
                child: Text(
                  AppLocalizations.of(context)!.launchScreenLabel,
                  style: StyleValue.launchScreenLabelStyle,
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
